package com.eapi.project.controller;

import com.eapi.project.service.UserService;
import com.eapi.project.common.BaseResponse;
import com.eapi.project.common.ResultUtils;
import com.eapi.project.utils.alioss.AliOSSUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

@Slf4j
@RestController
public class UpLoadController {
    /*@PostMapping("/upload")
    public Result upload(String username, Integer age, MultipartFile image) throws IOException {
        log.info("参数为{},{},{}",username,age,image);
        //获取到上传文件的名称
        String originalFilename = image.getOriginalFilename();
        //获取到后缀名
        int index = originalFilename.lastIndexOf(".");
        //生成唯一的文件名
        String fileName = UUID.randomUUID().toString() + originalFilename.substring(index);
        log.info("存储文件名称为：" + fileName);
        image.transferTo(new File("F:\\images\\" + fileName));
        *//*return Result.success();*//*
        return Result.success();
    }*/
    @Resource
    private AliOSSUtils aliOSSUtils;

    @Resource
    private UserService userService;
    @PostMapping("/upload")
    public BaseResponse<String> upload(MultipartFile image, HttpServletRequest request) throws IOException {
        log.info("上传文件名称为：{}",image.getOriginalFilename());
        String userAvatar = aliOSSUtils.upload(image);
        log.info("文件的url为：{}",userAvatar);
        /*User loginUser = userService.getLoginUser(request);
        loginUser.setUserAvatar(url);
        userService.updateById(loginUser);*/
        return ResultUtils.success(userAvatar);
    }
}
